package net.ix.util.net;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class Post{
	private static String tag = "Post";
	
	public static String UNICODECHARSET = "UTF-8";
	private UrlEncodedFormEntity postEntity;
	private List<NameValuePair> queryParams;
	private HttpPost post;
	
	private String uriStr;
	private URI uri;
	private int statusCode;
	private HttpEntity entity;
	private String entityContent;
	
	public Post(){
	}

	public Post(URI uri){
		setUri(uri);
	}
	public Post(String uriString){
		setUri(uriString);
	}
	public Post(String uriString, Map<String, String> querys){
		this(uriString);
		setQuerys(querys);
	}
	public Post(URI uri, Map<String, String> querys){
		this(uri);
		setQuerys(querys);
	}
	
	public void setQuerys(Map<String, String> querys){
		queryParams = new ArrayList<NameValuePair>();
		Iterator<Entry<String, String>> iter = querys.entrySet().iterator();
		
		while (iter.hasNext()) {
			Map.Entry<String, String> entry = iter.next();
			queryParams.add(new BasicNameValuePair(entry.getKey(), (String) entry.getValue()));
		}
		
		try {
			postEntity = new UrlEncodedFormEntity(queryParams, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public void setUri(URI u){
		uri = u;
	}
	public void setUri(String u){
		uriStr = u;
		try {
			uri = new URI(uriStr);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}
	
	public void connect(){
		post = new HttpPost(uri);
		post.setEntity(postEntity);
		
		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		
		HttpResponse response;
		
		try {
			Log.i(tag, "executing request " + post.getRequestLine());
			response = httpclient.execute(post);
			Log.i(tag, "http status is " + response.getStatusLine().getStatusCode());
			
			statusCode = response.getStatusLine().getStatusCode();
			entity = response.getEntity();
			if(entity != null){
				entityContent = EntityUtils.toString(entity, UNICODECHARSET);
			}
		} catch (ClientProtocolException e) {
			Log.e(tag, e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			Log.e(tag, e.getMessage());
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
	}

	public int getStatusCode(){
		return statusCode;
	}
	public String getContent(){
		return entityContent;
	}
	public HttpEntity getEntity(){
		return entity;
	}
	
	
}